import {
    reqGetDetailData,reqPostCartorUpdate
} from '@/api'
// 引入生成游客临时身份的模块
import {getUUID} from "@/utils/uuid_token"

const state = {
    // 商品详情数据
    detailData: {},
    // 游客临时身份
    uuid_token:getUUID()
}

const mutations = {
    GETDETAILDATA(state, detailData) {
        state.detailData = detailData
    },
   
}

const actions = {
    //    获取商品详情页数据
    async getDetailData({
        commit
    }, skuId) {
        let result = await reqGetDetailData(skuId)
        // console.log(result)
        if (result.code == 200) {
            commit('GETDETAILDATA', result.data)
        }

    },
    // 将用户选择的商品添加到服务器中保存,服务器没有返回数据，因此不需要三连环存储数据
    async addorUpdatecart({commit},{skuId,skuNum}){
        let result = await reqPostCartorUpdate(skuId,skuNum);
        // console.log(result)
        if(result.code == 200){
            return "ok"
        }else{
            return Promise.reject(new Error('faile'))
        }
    }

}

const getters = {
    categoryView(state) {
        return state.detailData.categoryView || {}
    },
    skuInfo(state) {
        return state.detailData.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.detailData.spuSaleAttrList || []
    }
}

export default {
    state,
    mutations,
    actions,
    getters
}